<?php

namespace app\api\controller\v1;

use app\common\controller\Api;
use app\common\logic\OrderLogic;
use app\common\model\Score;
use app\common\model\ScoreImg;
use app\common\model\Member;
use app\common\model\Purchaser;
class Order extends Api
{
    
    public function index()
    {
        $params = paramForm();
        $logic = new OrderLogic();
        
        //各种订单的操作，下单之类的
        if(isset($params['action'])){
            switch ($params['action']){
                case 'add':
                    $rt = $this->save($params);
                    break;
                case 'change':
                    $this->update($params);
                    break;
                case 'check':
                    $this->getPayStatus($params);
                case 'score':
                    $this->score($params);
                case 'checkSend':
                    $this->checkSend($params);
                break;
                default:
                    break;
            }
        }
    
        //如果有id参数，则表示获取单个
        if(isset($params['id'])){
            $this->read($params);
            die;
        }
        
        $params['user_id'] = $this->getUserId();//补齐用户的参数
        $params['with'] = 'module';//关联购买表啊

        $lists = $logic->getList($params);
        if($lists){
            foreach ($lists as $order){
                $order->purchaser = $logic->getPurchaser($order->id);
            }
            success($lists);
        }else{
            error('没有符合的数据');
        }
        
    }
    
    /**
     * 评价
     */
    public function score($params){
    
        $params['user_id'] = $this->getUserId();
        $rule = [
            'purchaserId|购买记录id' => 'require',
            'user_id|用户id' => 'require',
            'star|评分' => 'require',
        ];
        $this->validate($params,$rule);
    
    
        $purchaser = Purchaser::get($params['purchaserId']);
    
        if($purchaser->score_id){
            error('不能重复评论');
        }
    
        if($purchaser->user_id != $params['user_id']){
            error('无权评论');
        }
    
    
        $logic = new OrderLogic();
        $order = $logic->findModel($purchaser->order_id);
        if($order->status !=4){
            error('订单状态不符合，只有待评价状态的订单才可以评价');
        }
    
        $score = new Score();
        $score->purchaser_id = $purchaser->id;
    
        $score->goods_id = $purchaser->goods_id;
        $score->mall_id = $purchaser->mall_id;
        $score->user_id = $params['user_id'];
        $score->star = $params['star'];
        $score->content = $params['content'];
        $rt1 = $score->save();
        
        $goods = \app\common\model\Goods::get($purchaser->goods_id);
        $goods->comments++;
        $goods->isUpdate()->save();
    
        $purchaser->score_id = $score->id;
        $rt2= $purchaser->isUpdate()->save();
        
        $filter =[
            'order_id'=>$purchaser->order_id,
            'score_id'=>'not null'
        ];
        
        $noScorePurchaserLen = Purchaser::where($filter)->field('id')->count();
        if($noScorePurchaserLen<1){
            $order->status = 5;
            $rt2 = $order->isUpdate()->save();
        }
        
    
        if($rt1&&$rt2){
            success($score->id);
        }else{
            error('评价失败');
        }
        
        
       
        
    }
    
    public function purchaser(){
        $purchaser = Purchaser::get(['user_id'=>$this->getUserId(),'id'=>paramForm('id',true)]);
        if(!$purchaser){
            error('购买记录不存在');
        }
        
        $purchaser->score;
        
        success($purchaser);
    }
    
    
    /**
     * 确认收货
     */
    public function checkSend($params){
        $params['user_id'] = $this->getUserId();
        $rule = [
            'id|订单id' => 'require',
            'user_id|用户id' => 'require',
        ];
        $this->validate($params,$rule);
        
        $logic = new OrderLogic();
        
        $order = $logic->findModel($params['id']);
        if($order->user_id != $params['user_id']){
            error('无权操作该订单');
        }
        
        $rt = $logic->changeOrderStatus($order->id,4);
        
        
        
        if($rt){
            success(null);
        }else{
            error('确认收货失败');
        }
    }
    
    /**
     * @param $params
     * @desc 校验订单是否支付成功
     */
    public function getPayStatus($params){
        if(!isset($params['out_trade_no'])){
            error('out_trade_no参数必传');
        }
        $logic = new OrderLogic();
        $pay = $logic->checkUnifiedOrderPayStatus($params['out_trade_no']);

        success($pay);


    }
    
   
    
    public function save($params){
        
        $rule=[
            'companyList|店铺'=>'require',
            'chooseAddress|收货地址'=>'require'
        ];
        $this->validate($params,$rule);
        $logic = new OrderLogic();
        $userId = $this->getUserId();
        //companlist
        
        $rt =true;
        $out_trade_no = 'no_'.$logic->createUuid();//搞个多订单通用，专门用来第三方支付的订单号
        
        //1.遍历所有的店
        foreach($params['companyList'] as $company){
            //成功就返回订单号

            $rtAddOneOrdr = $logic->addOrder($company,$params['chooseAddress'],$userId,$out_trade_no);
           if(!$rtAddOneOrdr)$rt=false;
        }
        
        if($rt){
            success(['out_trade_no'=>$out_trade_no]);
        }else{
            error('下单失败');
        }
    }
    
    

}
